package br.com.vti.leilao.view.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class EmailUtil implements Serializable {
    
    private static final long serialVersionUID = -2328116610973750133L;

    private String charset;
    private Session session;

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public void enviarEmailComAnexo(String de, String para, String assunto,
            String message, String anexo) throws EmailUtilException {
        try {
            MultiPartEmail email = new MultiPartEmail();
            preencherEmail(email, de, para, assunto, message);

            List<InternetAddress> normais = this.montaDestinatarios("");
            if (normais != null && normais.size() > 0) {
                email.setCc(normais);
            }

            List<InternetAddress> ocultos = this.montaDestinatarios("");
            if (ocultos != null && ocultos.size() > 0) {
                email.setBcc(ocultos);
            }

            if (anexo != null && !anexo.isEmpty()) {
                EmailAttachment anexoAtt = new EmailAttachment();
                anexoAtt.setPath(anexo);
                anexoAtt.setDisposition(EmailAttachment.ATTACHMENT);
                email.attach(anexoAtt);
            }

            email.send();
        } catch (EmailException e) {
            throw new EmailUtilException(e.getMessage(), e);
        } catch (AddressException e) {
            throw new EmailUtilException(e.getMessage(), e);
        }
    }

    private void preencherEmail(Email email, String de, String para, String assunto, String message) throws EmailException {
        email.setMailSession(getSession());
        if (de != null && !de.isEmpty()) {
            email.setFrom(de);
        } else {
            Properties p = getSession().getProperties();
            if (p.containsKey("mail.user")) {
                email.setFrom(p.getProperty("mail.user"));
            } else if (p.containsKey("mail.smtp.user")) {
                email.setFrom(p.getProperty("mail.smtp.user"));
            }
        }
        email.addTo(para);
        email.setSubject(assunto);
        email.setMsg(message);
        email.setSentDate(new Date());
        email.setCharset(getCharset());
        email.setContent(message,"text/html; charset=" + "\"ISO-8859-1\"");
    }

    private List<InternetAddress> montaDestinatarios(String destinatarios) throws AddressException {
        List<InternetAddress> lista = null;
        if (destinatarios != null && !destinatarios.isEmpty()) {
            String[] emails = destinatarios.split(";");
            lista = new ArrayList<InternetAddress>();
            for (String email : emails) {
                lista.add(new InternetAddress(email));
            }
        }
        return lista;
    }

    public void enviarEmailSimples(String de, String para, String assunto,
            String message) throws EmailUtilException {
        try {
            SimpleEmail email = new SimpleEmail();
            preencherEmail(email, de, para, assunto, message);
            email.send();
        } catch (EmailException e) {
            throw new EmailUtilException(e.getMessage(), e);
        }
    }
}
